package hk.ust.cse.comp3111.studentSchedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ViewCalendar_agenda extends Fragment {
	
	String[] columns;
	ListView myListView;
	long time;
	long oneDay=86400000L;
	List<String> pass = new ArrayList<String>();
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.calendar_agenda, container, false);
         
        return rootView;
    }
	
	@Override
	public void onStart() {
		super.onStart();
		myListView = (ListView) this.getView().findViewById(R.id.listABC);
		ArrayAdapter<String> adapter;
		columns = new String[] { "calendar_id", "title", "description", "event_id","eventLocation", "startMinute", "endMinute"};
		
		Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
		//ContentUris.appendId(eventsUriBuilder, 1398268800000L);//24
		//ContentUris.appendId(eventsUriBuilder, 1398355199000L);//24
		
		ArrayList<String> listItems=new ArrayList<String>();
		time = Calendar.getInstance().getTimeInMillis();//
		Cursor cur = null;  

		//Toast.makeText(this.getActivity().getApplicationContext(), Long.toString(time), Toast.LENGTH_LONG).show();

		for(int i=0 ; i<10; i++){
			
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			long milliSeconds= time+i*oneDay;
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(milliSeconds);
			
			Uri.Builder eventsUriBuilder1 = CalendarContract.Instances.CONTENT_URI.buildUpon();
			
			ContentUris.appendId(eventsUriBuilder1, time+oneDay*i);
			ContentUris.appendId(eventsUriBuilder1, time+oneDay*i+oneDay-1000);
			Uri eventsUri = eventsUriBuilder1.build();     
			cur = this.getActivity().getContentResolver().query(eventsUri, columns, null, null, CalendarContract.Instances.DTSTART + " ASC");
			
			
			
			if (cur.moveToFirst()) {
				listItems.add(df.format(calendar.getTime()).toString());
				pass.add("");
				pass.add("");
				pass.add("");
	            do {
	            	listItems.add(cur.getString(1)+"\n"+
	            			timeCoversation(Long.parseLong(cur.getString(5)))+"-"+timeCoversation(Long.parseLong(cur.getString(6)))+"\n"+
	            			cur.getString(4));
	            	
	            	pass.add(cur.getString(3));
	            	pass.add(String.valueOf(time+oneDay*i));
	            	pass.add(String.valueOf(time+oneDay*i+oneDay-1000));
	            } while (cur.moveToNext());
	        }
			
		
		}
		

		adapter=new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1,listItems);
		myListView.setAdapter(adapter);
		
		myListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

				//Toast.makeText(getActivity(),"arg1 "+pass.get(3*position).toString(),Toast.LENGTH_SHORT).show();
			push(pass.get(3*position).toString(),pass.get(3*position+1).toString(),pass.get(3*position+2).toString());
			
			
			            }
			        });
	
	}// end onStart
	
	private void push(String eventID,String start, String end) {
    	Bundle save=new Bundle();
		save.putString("eventID",eventID);
		save.putString("start", start);
		save.putString("end", end);
		Fragment fragment = new ViewCalendar_singleItem();
		fragment.setArguments(save); 
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.frame_container, fragment).commit();
		
	}
	
	private String timeCoversation(double time){
		String hr, minute;
		String output;
		
		hr = Integer.toString((int) time/60);
		minute = Integer.toString((int) time%60);
		
		if(hr.length()==1) hr="0"+hr;
		if(minute.length()==1) minute=minute+"0";
	
		return new String(hr+minute);
		
		
	}
	

}
